package com.testplatfrom.autoapi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatfrom.autoapi.domain.TestplatfromTree;
import com.testplatfrom.autoapi.domain.dto.tree.TestplatfromTreeAdd;
import com.testplatfrom.autoapi.domain.dto.tree.TestplatfromTreeUpdate;
import com.testplatfrom.autoapi.domain.dto.tree.TestplatfromTreeVO;
import com.testplatfrom.autoapi.mapper.TestplatfromTreeMapper;
import com.testplatfrom.autoapi.service.TestplatfromTreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testplatfrom.common.core.domain.R;
import com.testplatfrom.common.core.exception.ServiceException;
import com.testplatfrom.common.core.utils.bean.BeanUtils;
import com.testplatfrom.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-12-11
 */
@Service
public class TestplatfromTreeServiceImpl extends ServiceImpl<TestplatfromTreeMapper, TestplatfromTree> implements TestplatfromTreeService {

    private List<TestplatfromTreeVO> allTestplatfromTreeVOs=new ArrayList<>();

    @Override
    public List<TestplatfromTreeVO> getTree() {
        //获取全部列表
        List<TestplatfromTree> testplatfromTreeVOs = this.list();
        for (TestplatfromTree testplatfromTree : testplatfromTreeVOs) {
            TestplatfromTreeVO testplatfromTreeVO = TestplatfromTreeVO.builder().build();
            BeanUtils.copyProperties(testplatfromTree,testplatfromTreeVO);
            this.allTestplatfromTreeVOs.add(testplatfromTreeVO);
        }
        if (CollectionUtils.isEmpty(allTestplatfromTreeVOs)){
            return new ArrayList<>();
        }
        List<TestplatfromTreeVO> testplatfromTreeVOS = allTestplatfromTreeVOs.stream().filter(testplatfromTreeVO -> testplatfromTreeVO.getParentId().equals("0")).collect(Collectors.toList());
        List<TestplatfromTreeVO> testplatfromTreeVOList = buildTree(testplatfromTreeVOS);
        return testplatfromTreeVOList;
    }

    public List<TestplatfromTreeVO> buildTree(List<TestplatfromTreeVO> testplatfromTreeVOS){
        // treeNodes：保存一个顶级节点所构建出来的完整树形
        List<TestplatfromTreeVO> treeNodes = new ArrayList<>();
        // getRootNode()：获取所有的根节点
        for (TestplatfromTreeVO treeRootNode : testplatfromTreeVOS) {
            // 将顶级节点进行构建子树
            treeRootNode = buildChildTree(treeRootNode);
            // 完成一个顶级节点所构建的树形，增加进来
            treeNodes.add(treeRootNode);
        }
        return treeNodes;
    }

    public TestplatfromTreeVO buildChildTree(TestplatfromTreeVO pNode){
        List<TestplatfromTreeVO> childTree = new ArrayList<>();
        // nodeList：所有节点集合（所有数据）
        for (TestplatfromTreeVO treeNode : allTestplatfromTreeVOs) {
            // 判断当前节点的父节点ID是否等于根节点的ID，即当前节点为其下的子节点
            if (treeNode.getParentId().equals(pNode.getId())) {
                // 再递归进行判断当前节点的情况，调用自身方法
                childTree.add(buildChildTree(treeNode));
            }
        }
        // for循环结束，即节点下没有任何节点，树形构建结束，设置树结果
        pNode.setChildren(childTree);
        return pNode;
    }

    @Override
    public Boolean addTrre(TestplatfromTreeAdd testplatfromTreeAdd) {
        TestplatfromTree testplatfromTree = this.getOne(new LambdaQueryWrapper<TestplatfromTree>().eq(TestplatfromTree::getParentId, testplatfromTreeAdd.getParentId()));
        if (ObjectUtil.isEmpty(testplatfromTree)){
            throw new ServiceException("父级节点不存在");
        }
        TestplatfromTree testplatfromTreeNew = TestplatfromTree.builder()
                .treeName(testplatfromTreeAdd.getTreeName())
                .parentId(testplatfromTreeAdd.getParentId())
                .createBy(SecurityUtils.getUsername())
                .createTime(LocalDateTime.now())
                .updateBy(SecurityUtils.getUsername())
                .updateTime(LocalDateTime.now())
                .build();
        return this.save(testplatfromTreeNew);
    }

    @Override
    public Boolean updateTrre(TestplatfromTreeUpdate testplatfromTreeUpdate) {
        TestplatfromTree testplatfromTree = this.getOne(new LambdaQueryWrapper<TestplatfromTree>().eq(TestplatfromTree::getParentId, testplatfromTreeUpdate.getId()));
        if (ObjectUtil.isEmpty(testplatfromTree)){
            throw new ServiceException("该节点不存在");
        }
        testplatfromTree.setTreeName(testplatfromTreeUpdate.getTreeName());
        testplatfromTree.setUpdateBy(SecurityUtils.getUsername());
        testplatfromTree.setUpdateTime(LocalDateTime.now());
        return this.updateById(testplatfromTree);
    }

}
